<!--
 * @FilePath: /web-vue/src/views/Region/chooseForm.vue
 * @Description: 【地区】新增表单
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="add-dialog"
      title="来源地调查"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :fullscreen="false"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addForm"
        status-icon
        label-width="150px"
        style="width: 100%"
        class="layer-form-style"
      >
        <el-form-item label="地区：" prop="parentId" size="mini">
          <el-cascader
            v-model="addForm.parentId"
            :options="regionList"
            :props="{
              expandTrigger: 'hover',
              checkStrictly: true,
              emitPath: false,
              value: 'id',
              label: 'name',
            }"
            placeholder="请选择来自哪个地区"
            filterable
            clearable
            style="width: 99%"
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      addForm: {
        parentId: null,
        // 辅助属性
      },
      // 表单校验规则
      rules: {
        parentId: [
          {
            required: true,
            message: "请选择地区！",
            trigger: "blur",
          },
        ],
      },
      regionList: [],
    };
  },

  methods: {
    // 打开【地区】表单新增窗口
    openAddForm(isShow) {
      this.visible = isShow;

      // 7）获取地区列表
      this.$api
        .queryRegionTreeList()
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            this.regionList = data;
          } else {
            this.$alert(`${message}`, "地区列表", {
              type: "error",
              confirmButtonText: "确定",
              callback: (action) => {},
            });
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【地区列表】查询接口发生错误：${err}`);
        });
    },

    // 关闭【地区】表单新增窗口
    handleCancel() {
      this.visible = false;
      this.$refs["addForm"].resetFields();

      // 清空上传文件列表
    },

    // 提交
    handleSubmit() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".add-dialog"),
            lock: true,
            text: "正在进行风险检测，请耐心等待！",
          });

          // 发起请求
          this.$api
            .queryOneRegion(this.addForm.parentId)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit", data);
                this.handleCancel();
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【地区】检测", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch((err) => {
              loading.close();
              this.visible = false;
              console.log(`【地区】检测接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
